/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.abstracts;

import java.util.ArrayList;
import java.util.List;
import org.balisunrise.common.Bindable;
import org.balisunrise.common.BindingGroup;
import org.balisunrise.common.FieldLayout;

/**
 *
 * @author Glauco
 */
public abstract class AbstractLayoutable extends AbstractVisualEntity{

    protected List<FieldLayout> layouts;
    public static final String PROP_LAYOUTS = "layouts";

    public AbstractLayoutable() {
        layouts = new ArrayList<FieldLayout>();
    }

    /**
     * Get the value of layouts
     *
     * @return the value of layouts
     */
    public List<FieldLayout> getLayouts() {
        return layouts;
    }

    /**
     * Set the value of layouts
     *
     * @param layouts new value of layouts
     */
    public void setLayouts(List<FieldLayout> layouts) {
        List<FieldLayout> oldLayouts = this.layouts;
        this.layouts = layouts;
        changeSupport.firePropertyChange(PROP_LAYOUTS, oldLayouts, layouts);
    }

    public FieldLayout[] getFieldLayouts(){
        return layouts.toArray(new FieldLayout[layouts.size()]);
    }

    public void addFieldLayout(FieldLayout fl) {
        layouts.add(fl);
    }

    public boolean removeFieldLayout(FieldLayout fl) {
        return layouts.remove(fl);
    }

    /**
     * Requere o focus para o primeiro FieldComponent.
     */
    public void requestFocus(){
        for (FieldLayout fl : layouts) {
            if(fl.requestFocus())
                return ;
        }
    }

    /**
     * Cria Bindings para os layouts que são Bindable
     */
    public void createBindings(BindingGroup bg,Object source, String property){
        for (FieldLayout fl : layouts) {
            if(fl instanceof Bindable){
                Bindable bindable = (Bindable) fl;
                bindable.bindComponent(bg, source, property);
            }
        }
    }
}
